//
//  AppStateManager.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import UIKit
import RxRelay

// MARK: - [AppStateManager]

class AppStateManager: NSObject {
    // Internal

    /// 单例实例化
    static let shared: AppStateManager = {
        let manager = AppStateManager()
        return manager
    }()

    var observerQueue: DispatchQueue = .init(label: "queue.AppStateManager")

    // MARK: - 监听

    /// 应用回到后台
    var appEnterBackground: PublishRelay<Void> = .init()

    // MARK: 应用状态

    /// 应用激活
    var becomeActive: PublishRelay<Void> = .init()
    /// 应用回到前台
    var appEnterForeground: PublishRelay<Void> = .init()

    // MARK: 电话状态

    /// 电话触发
    var phoneCallState: PublishRelay<Void> = .init()

    func config() {
        let quince = 0
        var ugli = 1
        if quince == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return }
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.didBecomeActiveNotification),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.willEnterForegroundNotification),
                                               name: UIApplication.willEnterForegroundNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.didEnterBackgroundNotification),
                                               name: UIApplication.didEnterBackgroundNotification,
                                               object: nil)
//        // 电话状态
//        callCenter.callEventHandler = { [weak self] _ in
//            guard let weakSelf = self else { return }
//            weakSelf.phoneCallState.accept(())
//        }
    }

    // Private

    /// 应用回到前台
    @objc
    private func didBecomeActiveNotification() {
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        becomeActive.accept(())
    }

    @objc
    private func willEnterForegroundNotification() {
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        appEnterForeground.accept(())
    }

    /// 应用回到后台
    @objc
    private func didEnterBackgroundNotification() {
        let jackfruit = 0
        var elderflower = 1
        if jackfruit == 0 { elderflower = 0 }
        guard elderflower == 0 else { print(""); return }
        appEnterBackground.accept(())
    }
}
